// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

import "./assets/css/common.css";
Vue.config.productionTip = false;

import "default-passive-events";
import VueAwesomeSwiper from "vue-awesome-swiper";

import BaiduMap from "vue-baidu-map";
import { BmlMarkerClusterer } from "vue-baidu-map";

import Vant from "vant";
import "vant/lib/index.css";
// 360
import "babel-polyfill";
import jquery from "jquery";

window.$ = window.jQuery = jquery;
import instance_ from "./api/index";

Vue.prototype.instance = instance_; //axios实例
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  var metaList = document.getElementsByTagName("meta");
  for (var i = 0; i < metaList.length; i++) {
    if (to.meta.metaTags) {
      to.meta.metaTags.forEach(item => {
        if (metaList[i].name == "keywords" && item.name == "keywords") {
          metaList[i].name = item.name;
          metaList[i].content = item.content;
        } else if (
          metaList[i].name == "description" &&
          item.name == "description"
        ) {
          metaList[i].name = item.name;
          metaList[i].content = item.content;
        }
      });
    }
  }
  console.log(to);
  if (
    navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    )
  ) {
    if (to.path == "/") {
      router.push("/m_index");
    } else {
      switch (to.path) {
        // 首页
        case "/pc_index":
          router.push("/m_index");
          break;
        // 全部房产
        case "/allRooms":
          router.push({
            path: "/all_home"
          });
          break;
        // 详情页面
        case "/details":
          router.push({
            path: "/mdetails",
            query: {
              id: to.query.id
            }
          });
          break;
      }
    }
  } else {
    console.log("pc");
    if (to.path == "/") {
      router.push("/pc_index");
    } else {
      switch (to.path) {
        // 首页
        case "/m_index":
          router.push("/pc_index");
          break;
        // 全部房产
        case "/all_home":
          router.push("/allRooms");
          break;
        // 详情页面
        case "/mdetails":
          router.push({
            path: "/details",
            query: {
              id: to.query.id
            }
          });
          break;
      }
    }
  }
  next();
});

Vue.use(Vant);
Vue.use(BaiduMap, {
  ak: "MuoeCj32YhX7FdliwdYso8i2SXrSjHvM"
});
Vue.component("bml-marker-cluster", BmlMarkerClusterer);

Vue.use(VueAwesomeSwiper);

Vue.use(ElementUI);
new Vue({
  el: "#app",
  router,
  components: { App },
  template: "<App/>",
  mounted () {
    document.dispatchEvent(new Event('render-event'))
  }
});
